package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.common.enums.GroundingStatus;
import com.itheima.wms.dto.GroundingDTO;
import com.itheima.wms.dto.GroundingDistributeDTO;
import com.itheima.wms.entity.GroundingEntity;
import com.itheima.wms.service.GroundingService;
import com.itheima.wms.service.StatusRecordService;
import com.itheima.wms.vo.BatchVO;
import com.itheima.wms.vo.GroundingDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 上架任务
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/grounding")
@Api(value = "Grounding", tags = "上架任务")
public class GroundingController extends BaseController {

    @Autowired
    private GroundingService groundingService;

    @Autowired
    private StatusRecordService statusRecordService;

    /**
     * 分页查询上架任务
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询上架任务", notes = "分页查询上架任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "上架任务单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "receiptCode", value = "入库单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "personName", value = "负责人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planNum", value = "预计到货数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "realNum", value = "实收货数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "groundingNum", value = "上架数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "differenceNum", value = "数量差异", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "completionTime", value = "完成时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "masterId", value = "上级id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "上架情况 1待分配、2上架中、3上架完成、4已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<GroundingEntity>> page(@RequestParam Map data) {
        Page<GroundingEntity> page = getPage();
        IPage<GroundingEntity> groundingPage = groundingService.page(page, data);
        page.setRecords(groundingPage.getRecords());
        return success(page);
    }


    @ApiOperation(value = "分页查询上架任务明细", notes = "分页查询上架任务明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "code", value = "任务单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "receiptCode", value = "入库单号", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "上架情况 1待分配、2上架中、3上架完成、4已取消", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageDetail")
    public R<IPage<GroundingDetailVO>> pageDetail(@RequestParam Map data) {
        Page<GroundingDetailVO> page = getPage();
        IPage<GroundingDetailVO> groundingDetailVOIPage = groundingService.pageDetail(page, data);
        page.setRecords(groundingDetailVOIPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询全部上架任务", notes = "查询全部上架任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "上架任务单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "receiptCode", value = "入库单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "personName", value = "负责人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planNum", value = "预计到货数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "realNum", value = "实收货数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "groundingNum", value = "上架数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "differenceNum", value = "数量差异", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "completionTime", value = "完成时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "masterId", value = "上级id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "上架情况 1待分配、2上架中、3上架完成、4已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<GroundingEntity>> list(@RequestParam Map data) {
        List<GroundingEntity> groundingArray = groundingService.list(data);
        return success(groundingArray);
    }

    /**
     * 查询上架任务
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询上架任务", notes = "查询上架任务")
    @GetMapping("/{id}")
    public R<GroundingEntity> get(@PathVariable Long id) {
        return success(groundingService.getById(id));
    }

    /**
     * 新增上架任务
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增上架任务", notes = "新增上架任务不为空的字段")
    @PostMapping
    public R<GroundingEntity> save(@RequestBody @Validated(SuperEntity.Save.class) GroundingDTO data) {
        groundingService.save(data);
        statusRecordService.saveStatus(data);
        return success(data);
    }

    /**
     * 修改上架任务
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改上架任务", notes = "修改上架任务不为空的字段")
    @PutMapping
    public R<GroundingEntity> update(@RequestBody @Validated(SuperEntity.Update.class) GroundingDTO data) {
        groundingService.updateById(data);
        return success(data);
    }

    /**
     * 分配
     *
     * @param data 分配对象
     * @return 分配结果
     */
    @ApiOperation(value = "分配", notes = "分配")
    @PutMapping("distribute")
    public R<Boolean> distribute(@RequestBody @Validated GroundingDistributeDTO data) {
        GroundingEntity groundingEntity = new GroundingEntity();
        groundingEntity.setStatus(GroundingStatus.GROUNDING.getCode());
        groundingEntity.setId(data.getId());
        groundingEntity.setPersonName(data.getPersonName());
        statusRecordService.saveStatus(groundingEntity);
        return success(groundingService.updateById(groundingEntity));
    }

    /**
     * 上架完成
     *
     * @param ids
     * @return 上架完成结果
     */
    @ApiOperation(value = "上架完成", notes = "上架完成")
    @PutMapping("complete")
    public R<BatchVO> complete(@RequestBody List<Long> ids) {
        return success(groundingService.complete(ids));
    }


    /**
     * 删除上架任务
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除上架任务", notes = "根据id物理删除上架任务")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        groundingService.removeByIds(ids);
        return success();
    }

}
